﻿// ***********************************************************************
// Apache 2.0 License
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Models
// File      : LikeHelper.cs
//
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq;

namespace ContractMeow.Models.Helpers
{
	/// <summary>
	/// Class LikeHelper
	/// </summary>
	public static class LikeHelper
	{
		/// <summary>
		/// Creates the liked by string.

		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="userId">The user id.</param>
		/// <returns>System.String.</returns>
		public static string CreateLikedByString(Message message, long userId)
		{
			//todo: localization
			if (message == null || message.LikeCount < 1)
			{
				return null;
			}
			//Basic formula: you + others with names + others by count
			IList<Like> likesFromOtherUsers = message.Likes
													 .Where(like => like.UserId != userId)
													 .ToList();

			IList<String> listOfUsernames = new List<String>();

			if (message.IsLiked)
			{
				listOfUsernames.Add("You");
			}

			listOfUsernames = listOfUsernames
				.Concat(likesFromOtherUsers.Select(like => like.FullName))
				.ToList();

			Int32 unnamedOthers = message.LikeCount - listOfUsernames.Count;

			if (unnamedOthers > 0)
			{
				return String.Format(unnamedOthers == 1 ? "{0} and {1} other" : "{0} and {1} others", String.Join(", ", listOfUsernames), unnamedOthers);
			}
			return listOfUsernames.Count > 1
					   ? String.Format("{0} and {1}", String.Join(", ", listOfUsernames.Take(listOfUsernames.Count - 1)),
									   listOfUsernames.Last())
					   : listOfUsernames.First();
		}
	}
}